package a07_二叉树;

/**
 * <p>
 * a29_二叉搜索树中的插入操作
 * </p>
 *
 * @author flyduck
 * @since 2024/12/25
 */
public class a29_二叉搜索树中的插入操作 {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) {
            return new TreeNode(val);
        }
        traversal(root, val);
        return root;
    }

    public void traversal(TreeNode currentNode, int val){
        if (currentNode == null) {
            return;
        }

        if(val < currentNode.val){
            if(currentNode.left == null){
                currentNode.left = new TreeNode(val);
            }else {
                traversal(currentNode.left,val);
            }
        }

        if(val > currentNode.val){
            if(currentNode.right == null){
                currentNode.right = new TreeNode(val);
            }else {
                traversal(currentNode.right,val);
            }
        }
    }
}
